<?php

namespace addons\kzvote\controller\store;

use app\common\model\kzvote\music\Music;
use app\common\model\kzvote\Person as PersonModel;
use app\common\model\kzvote\Vote as VoteModel;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Person extends BaseController
{

    /**
     * 查看
     */
    public function index()
    {
        $user = $this->auth->getUser();
        $votes = VoteModel::where('user_id', $user['id'])->where('mold','<',4)
            ->where('status','<',30)->field('id,name')->select();

        if ($this->request->isAjax()) {
            $params = $this->request->param();
            $vote_ids = array_column((array)$votes,'id');
            $where['vote_id'] = ['in', $vote_ids];

            if ($params['vote_id']){
                if (!in_array($params['vote_id'], $vote_ids)){
                    $this->error('请筛选自己的活动');
                }
                $where['vote_id'] = $params['vote_id'];
            }
            if ($params['group_name']){
                $where['group_name'] = $params['group_name'];
            }
            if ($params['code']){
                $where['code'] = $params['code'];
            }
            if ($params['name']){
                $where['name'] = $params['name'];
            }
            if ($params['status']){
                $where['status'] = $params['status'];
            }
            if ($params['type'] || $params['type'] === '0'){
                $where['type'] = $params['type'];
            }
            $list = PersonModel::where($where)
                ->order('id desc')
                ->paginate(10);
            foreach ($list as &$item){
                $item['vote_name'] = VoteModel::where('id', $item['vote_id'])->value('name');
            }

            $result = array("total" => $list->total(), "rows" => $list->items());
            $this->success('','',$result);
        }
        $music = Music::where('status', '10')
            ->order('weigh', 'desc')
            ->field('id,name')
            ->select();
        $this->view->assign('music', $music);
        $this->view->assign('votes', $votes);
        return $this->view->fetch();
    }


}
